//Start functions of Parameter
var ParameterTemplate = "templates/Parameter.xsl"

function loadParameterData(xID){
	if(objectHTTPXML) {
		xIDTmp = xID
		waitBox = new Lightbox.base('box1');
		objectHTTPXML.open('GET', '/model.load.parameter?id=' + xID + '&m_id=' + modelID , true)
		objectHTTPXML.onreadystatechange = returnParameterData
		objectHTTPXML.send(null)
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnParameterData(){
	if (objectHTTPXML.readyState == 4) {
		xml = objectHTTPXML.responseXML
		template = loadXSLfromXML(ParameterTemplate, xml)
		document.getElementById("divBody").innerHTML = template
		waitBox.hideBox(null);
		insertTypePa()
	}			
}

function saveParameter(xID){
	if(objectHTTPXML) {
		var parameters = "m_id=" + modelID +
						 "&id=" + encodeURI(document.getElementById("id").value) +
						 "&name=" + encodeURI(document.getElementById("name").value) +
						 "&lower=" + encodeURI(document.getElementById("lower").value) +
						 "&upper=" + encodeURI(document.getElementById("upper").value) +
						 "&type=" + encodeURI(document.getElementById("type").value)
		
		if(document.getElementById("isOrdered").checked){
			parameters += "&isOrdered=" + 1
		}
		if(document.getElementById("isUnique").checked){
			parameters += "&isUnique=" + 1
		}
		
		var raisedException = document.getElementsByName("raisedException")
		
		if(raisedException != null){
			for(var i = 0; i < raisedException.length; ++i){
				parameters += "&raisedException=" + encodeURI(raisedException[i].value)				
			}
		}

		xIDTmp = xID

		waitBox = new Lightbox.base('box1');

		objectHTTPXML.onreadystatechange = returnSaveParameter
		objectHTTPXML.open('POST', '/model.save.parameter', true);
		objectHTTPXML.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		objectHTTPXML.setRequestHeader("Content-length", parameters.length);
		objectHTTPXML.setRequestHeader("Connection", "close");
		objectHTTPXML.send(parameters);
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnSaveParameter(){
	if (objectHTTPXML.readyState == 4) {
		waitBox.hideBox(null);

		if(objectHTTPXML.responseText == 'OK'){
			changeName(xIDTmp, document.getElementById("name").value)
			alert('Data saved!')
		} else {
			alert('Data not saved!')
			loadParameterData(xIDTmp)
		}
	}			
}

function createParameter(xID){
	if(objectHTTPXML) {
		xIDTmp = xID
		waitBox = new Lightbox.base('box1');
		objectHTTPXML.open('GET', '/model.create.parameter?&m_id=' + modelID + '&p_id=' + xID , true)
		objectHTTPXML.onreadystatechange = returnParameter
		objectHTTPXML.send(null)
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnParameter(){
	if (objectHTTPXML.readyState == 4) {
		if(objectHTTPXML.responseText.split("$$$$").length != 0){
			id = objectHTTPXML.responseText.split("$$$$")
			createNewNode(xIDTmp, id[0], id[1], 'javascript:loadParameterData("' + id[0] + '");', 'parameter.gif', 'parameter.gif')
		} else {
			alert('Error to create the Parameter!')
		}
		waitBox.hideBox(null);
	}			
}

function insertTypePa(){
	if(objectHTTPXML) {
		waitBox = new Lightbox.base('box1');
		objectHTTPXML.open('GET', '/model.load.alltypes?&m_id=' + modelID, true)
		objectHTTPXML.onreadystatechange = returnInsertTypePa
		objectHTTPXML.send(null)
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnInsertTypePa(){
	if (objectHTTPXML.readyState == 4) {
		xml = objectHTTPXML.responseXML
		template = loadXSLfromXML(getTypesTemplate, xml)
		document.getElementById("divType").innerHTML = template

		var typeId = document.getElementById("typeId").value
		var type = document.getElementById("type")
		
		for(i = 0; i < type.options.length; ++i){
			if(type.options[i].value == typeId){
				type.selectedIndex = i
				break
			}
		}
		
		waitBox.hideBox(null);
	}			
}
//End functions of Parameter